/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package test.wizard;

import org.springframework.binding.validation.ValidationListener;
import org.springframework.binding.validation.ValidationResults;
import org.springframework.binding.validation.support.RulesValidator;
import org.springframework.core.io.AbstractResource;
import org.springframework.richclient.application.setup.SetupWizard;
import org.springframework.richclient.command.ActionCommandExecutor;
import org.springframework.richclient.form.AbstractForm;
import org.springframework.richclient.form.FormModelHelper;
import org.springframework.richclient.form.builder.TableFormBuilder;
import org.springframework.richclient.wizard.AbstractWizardPage;
import org.springframework.richclient.wizard.FormBackedWizardPage;
import org.springframework.richclient.wizard.WizardDialog;
import org.springframework.richclient.dialog.DialogPage;
import org.springframework.rules.Rules;
import org.springframework.rules.factory.Constraints;
import org.springframework.rules.support.DefaultRulesSource;

import javax.swing.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import java.beans.PropertyChangeEvent;

public class NewAdminReportWizard extends SetupWizard implements ActionCommandExecutor
{

//    private ExtraPage extraPage;
    private LastPage lastPage;

    private WizardDialog dialog;

    public NewAdminReportWizard()
    {
//        extraPage = new ExtraPage(this);
        lastPage = new LastPage();
        addPages();
//        setLicenseTextLocation(new LicenseResource());
        setLicenseTextLocation(new LicenseResource());
//        addPage(extraPage);
        addPage(lastPage);
    }

    public boolean canFinish()
    {
        
        return dialog.getCurrentPage().getId().equals("lastPage");
    }
    
    

    public void execute()
    {
        if (dialog == null)
        {
            dialog = new WizardDialog(this);
        }
        dialog.showDialog();
    }

    public WizardDialog getDialog()
    {
        return dialog;
    }

    
    class LicenseResource extends AbstractResource
    {

        public String getDescription()
        {
            return "lastPage";
        }

        public InputStream getInputStream() throws IOException
        {
            return new StringBufferInputStream("Clique em prosseguir");
        }
    }
//
//    class ExtraPage extends FormBackedWizardPage
//    {
//        private boolean ready;
//
//        public ExtraPage(final NewAdminReportWizard wizard)
//        {
//            super(new ExtraForm());
//            getBackingForm().addValidationListener(new ValidationListener()
//            {
//                public void validationResultsChanged(ValidationResults results)
//                {
//                    boolean oldReady = ready;
//                    ready = !results.getHasErrors();
//                    try {
//                        wizard.getDialog().propertyChange(new PropertyChangeEvent(this, DialogPage.PAGE_COMPLETE_PROPERTY, oldReady, ready));
//                    } catch(NullPointerException ex)
//                    {
//                        // could happen, ignore
//                    }
//                }
//            });
//        }
//
//        public void setReady(boolean ready)
//        {
//            this.ready = ready;
//        }
//
//        public boolean isPageComplete()
//        {
//            return ready;
//        }
//    }
//
    class LastPage extends AbstractWizardPage
    {
        protected LastPage()
        {
            super("lastPage");
        }

        protected JComponent createControl()
        {
            return new JLabel("Parabéns, relatório criado com sucesso!");
        }

        public boolean canFlipToNextPage()
        {
            return false;
        }
    }
//
//    class ExtraForm extends AbstractForm
//    {
//
//        public ExtraForm()
//        {
//            super(FormModelHelper.createFormModel(new ExtraObject(), "extraForm"));
//            DefaultRulesSource source = new DefaultRulesSource();
//            Rules rules = new Rules(ExtraObject.class);
//            Constraints c = new Constraints();
//            rules.add("configOne", c.required());
//            rules.add("configTwo", c.required());
//            source.addRules(rules);
//            getFormModel().setValidator(new RulesValidator(getFormModel(), source));
//        }
//
//        protected JComponent createFormControl()
//        {
//            TableFormBuilder builder = new TableFormBuilder(getBindingFactory());
//            builder.add("configOne");
//            builder.add("configTwo");
//            return builder.getForm();
//        }
//    }
//
//    class ExtraObject
//    {
//        private String configOne;
//        private String configTwo;
//
//        public String getConfigOne()
//        {
//            return configOne;
//        }
//
//        public void setConfigOne(String configOne)
//        {
//            this.configOne = configOne;
//        }
//
//        public String getConfigTwo()
//        {
//            return configTwo;
//        }
//
//        public void setConfigTwo(String configTwo)
//        {
//            this.configTwo = configTwo;
//        }
//    }
}


